﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;

namespace AssemblyExplorer
{
    public partial class frmCode : Form
    {
        public frmCode()
        {
            InitializeComponent();
        }

        public string Code
        {
            get { return txtCode.Text; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    txtCode.Text = Properties.Resources.DefaultCode;
                else
                    txtCode.Text = value;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            var results = ReflectionHelper.Compile(txtCode.Text);
            StringBuilder sb = new StringBuilder();
            if (results.Errors.Count > 0)
            {
                foreach (CompilerError error in results.Errors)
                    sb.AppendLine(error.ErrorText);
            }
            else
            {
                sb.Append("Success!");
            }
            txtOutput.Text = sb.ToString();
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            txtCode.Text = Properties.Resources.DefaultCode;
        }
    }
}
